<template>
  <div class="app-container">
    <div class="app-container__section shrink-0">
      <plus-search
          v-bind="getSearchBinds()"
          :columns="columns"
      ></plus-search>
    </div>

    <div class="app-container__section">
      <plus-table
          v-bind="getBinds()"
          :columns="columns"
          :height="500"
      >
        <template #title>
          <table-title   />
        </template>
        <template #toolbar>
          <el-button
              type="primary"
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['system:dict:add']"
          >新增
          </el-button>
          <el-button
              type="danger"
              icon="Delete"
              :disabled="gSelection.length > 0"
              @click="handleDelete"
              v-hasPermi="['system:dict:remove']"
          >删除
          </el-button>
          <el-button
              type="warning"
              icon="Close"
              @click="handleClose"
          >关闭
          </el-button>
        </template>
        <template #plus-cell-dictLabel="scope">
          <span
              v-if="(scope.row.listClass == '' || scope.row.listClass == 'default') && (scope.row.cssClass == '' || scope.row.cssClass == null)">{{
              scope.row.dictLabel
            }}</span>
          <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass"
                  :class="scope.row.cssClass">{{ scope.row.dictLabel }}
          </el-tag>
        </template>
        <template #plus-cell-action="scope">
          <el-tooltip effect="dark" content="修改" placement="top">
            <el-button
                type="primary"
                link
                icon="EditPen"
                @click="handleUpdate(scope.row)"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top">
            <el-button
                type="danger"
                link
                icon="Delete"
                @click="handleDelete(scope.row)"
            >
            </el-button>
          </el-tooltip>
        </template>
      </plus-table>
    </div>

    <!-- 添加或修改参数配置对话框 -->
    <DialogView :title="title" v-model="open" @cancel="cancel" @submit="submitForm">
      <el-form ref="dataRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典类型">
          <el-input v-model="form.dictType" :disabled="true"/>
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签"/>
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入数据键值"/>
        </el-form-item>
        <el-form-item label="样式属性" prop="cssClass">
          <el-input v-model="form.cssClass" placeholder="请输入样式属性"/>
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="回显样式" prop="listClass">
          <el-select v-model="form.listClass">
            <el-option
                v-for="item in listClassOptions"
                :key="item.value"
                :label="item.label + '(' + item.value + ')'"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :value="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
    </DialogView>
  </div>
</template>

<script setup name="Data">
import useDictStore from "@/store/modules/dict";
import { optionselect as getDictOptionselect, getType, listType } from "@/api/system/dict/type";
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";
import { PlusTable } from "plus-pro-components";
import { useTablePage } from "@/hooks/useTablePage.js";
import { useRoute } from "vue-router";
import DialogView from "@/components/Viewers/DialogView.vue";
import TableTitle from "@/components/Table/TableTitle.vue";

const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");

const open = ref(false);
const ids = ref([]);
const title = ref("");
const defaultDictType = ref("");
const defaultDictData = ref({})
const typeOptions = ref([]);
const route = useRoute();

const columns = computed(() => {
  return [
    {
      label: "字典类型",
      prop: "dictType",
      valueType: "select",
      options: typeOptions.value,
      optionsMap: {
        label: "dictType",
        value: "dictType"
      },
      hideInTable: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典编码",
      prop: "dictCode",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典标签",
      prop: "dictLabel",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典键值",
      prop: "dictValue",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典排序",
      prop: "dictSort",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "状态",
      prop: "status",
      valueType: "select",
      options: sys_normal_disable.value,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "备注",
      prop: "remark",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "创建时间",
      prop: "createTime",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作",
      prop: "action",
      width: 100,
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    }
  ];
});

const searchForm = ref({});

const {
  gGetList,
  getBinds,
  gSelection,
  getSearchBinds
} = useTablePage(listData, {
  searchForm,
  init: false
});

// 数据标签回显样式
const listClassOptions = ref([
  {value: "default", label: "默认"},
  {value: "primary", label: "主要"},
  {value: "success", label: "成功"},
  {value: "info", label: "信息"},
  {value: "warning", label: "警告"},
  {value: "danger", label: "危险"}
]);

const data = reactive({
  form: {},
  rules: {
    dictLabel: [{required: true, message: "数据标签不能为空", trigger: "blur"}],
    dictValue: [{required: true, message: "数据键值不能为空", trigger: "blur"}],
    dictSort: [{required: true, message: "数据顺序不能为空", trigger: "blur"}]
  }
});

const {form, rules} = toRefs(data);

/** 查询字典类型详细 */
function getTypes(dictId) {
  getType(dictId).then(response => {
    searchForm.value.dictType = response.data.dictType;
    defaultDictType.value = response.data.dictType;
    defaultDictData.value = response.data
    gGetList();
  });
}

/** 查询字典类型列表 */
function getTypeList() {
  getDictOptionselect().then(response => {
    typeOptions.value = response.data;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  dialogFormReset();
}

/** 表单重置 */
function dialogFormReset() {
  form.value = {
    dictCode: undefined,
    dictLabel: undefined,
    dictValue: undefined,
    cssClass: undefined,
    listClass: "default",
    dictSort: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("dataRef");
}

/** 返回按钮操作 */
function handleClose() {
  const obj = {path: "/system/dict"};
  proxy.$tab.closeOpenPage(obj);
}

/** 新增按钮操作 */
function handleAdd() {
  dialogFormReset();
  open.value = true;
  title.value = "添加字典数据";
  form.value.dictType = searchForm.value.dictType;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  dialogFormReset();
  const dictCode = row.dictCode || ids.value;
  getData(dictCode).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改字典数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (valid) {
      if (form.value.dictCode != undefined) {
        updateData(form.value).then(response => {
          useDictStore().removeDict(searchForm.value.dictType);
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          gGetList();
        });
      } else {
        addData(form.value).then(response => {
          useDictStore().removeDict(searchForm.value.dictType);
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          gGetList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const dictCodes = row.dictCode || ids.value;
  proxy.$modal.confirm("是否确认删除字典编码为\"" + dictCodes + "\"的数据项？").then(function () {
    return delData(dictCodes);
  }).then(() => {
    gGetList();
    proxy.$modal.msgSuccess("删除成功");
    useDictStore().removeDict(searchForm.value.dictType);
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/dict/data/export", {
    ...searchForm.value
  }, `dict_data_${new Date().getTime()}.xlsx`);
}

getTypes(route.params && route.params.dictId);
getTypeList();
</script>
